package com;

import com.zyx.mapper.DbMapper;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("mybatis demo 全编程方式案例")
public class MybatisDemo {
    DbMapper dm;

    @BeforeEach
    void init(){
        //建立数据库连接池
        PooledDataSource ds = new PooledDataSource("com.mysql.cj.jdbc.Driver","jdbc:mysql:/db","root","123456");

        //事务对象
        TransactionFactory tx = new JdbcTransactionFactory();
        //开发环境对象
        Environment env = new Environment("development",tx,ds);
        Configuration cfg = new Configuration(env);
        cfg.addMapper(DbMapper.class);
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
        SqlSession session = sf.openSession();
        dm = session.getMapper(DbMapper.class);
    }

    @Test
    void t1(){
        System.out.println(dm.version());
        System.out.println(dm.tbs());
        System.out.println(dm.queryAll());
    }
}
